/**
 * @author
 * @createtime
 * @class ProjectAttendForm
 * @extends Ext.Window
 * @description ProjectAttend表单
 */
ProjectAttendForm = Ext.extend(Ext.Window, {
	//内嵌FormPanel
	formPanel : null,
	//构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		//必须先初始化组件
		this.initUIComponents();
		ProjectAttendForm.superclass.constructor.call(this, {
			id : 'ProjectAttendFormWin',
			layout : 'fit',
			items : this.formPanel,
			modal : true,
			width : 800,
			height : 600,
			maximizable : true,
			title : '[材料分包考勤记录]详细信息',
			buttonAlign : 'center',
			buttons : this.buttons
		});
	},//end of the constructor
	//初始化组件
	initUIComponents : function() {
		var thizId = this.id;
		var thiz = this;
		var summary = new Ext.ux.grid.GridSummary();
		var statusArr = [[0, '未填写'], [1, '到勤'], [2, '事假'], [3, '旷工'], [4, '迟到'], [5, '休息']];
		this.formPanel = new Ext.FormPanel({
			layout : 'form',
			trackResetOnLoad : true,
			bodyStyle : 'padding:10px 10px 10px 10px',
			border : false,
			url : __ctxPath + '/produce/saveProjectAttend.do',
			id : 'ProjectAttendForm',
			defaults : {
				anchor : '98%,98%'
			},
			defaultType : 'textfield',
			items : [{
						name : 'projectAttend.id',
						id : 'id',
						xtype : 'hidden',
						value : this.id == null ? '' : this.id
					}, {
						xtype : 'container',
						layout : 'column',
						height : 25,
						defaults : {
							xtype : 'label',
							style : {
								margin : '0px 0px 3px 0px'
							}
						},
						items : [{
									text : '项目名称：',
									width : 60
								}, {
									xtype : 'textfield',
									name : 'projectAttendProName',
									id : 'projectAttendProName',
									columnWidth : .4,
									allowBlank : false,
									emptyText : '请选择项目',
									listeners : {
										'focus' : function() {
											ProjectSelector.getView(function(proId, proName, proNo) {
												Ext.getCmp("projectAttend.project.id").setValue(proId);
												Ext.getCmp("projectAttendProName").setValue(proName);
											}, true).show();
										}
									}
								}, {
									xtype : "hidden",
									name : "projectAttend.project.id",
									id : "projectAttend.project.id"
								}, {
									text : '月份：',
									style : 'margin-left:10px;',
									width : 60
								}, {
									xtype : 'datefield',
									name : 'projectAttend.yearMonth',
									id : 'yearMonth',
									allowBlank : false,
									columnWidth : .3,
									format : 'Y-m'
								}]
					}, {
						xtype : 'container',
						layout : 'column',
						height : 25,
						defaults : {
							xtype : 'label',
							style : {
								margin : '0px 0px 3px 0px'
							}
						},
						items : [{
									text : '考勤详情：'
								}, {
									style : 'color:red',
									id : 'projectAttendErrorLabel',
									text : '(*必填)'
								}]
					}, {
						xtype : 'container',
						style : 'padding-left:0px;margin-bottom:10px',
						layout : 'column',
						items : [{
									name : 'resultGridData',
									xtype : 'hidden',
									id : 'projectAttend.resultGridData'
								}, {
									name : 'dataList',
									xtype : 'hidden',
									id : 'projectAttend.dataList'
								}, {
									xtype : 'grid',
									id : 'projectAttend.resultGridDataGrid',
									height : 350,
									width : '98%',
									stripeRows : true,
									trackMouseOver : true,
									clicksToEdit : 1,
									plugins : [new Ext.ux.grid.RowEditor({
												saveText : '保存',
												cancelText : '取消',
												errorText : '请输入完整信息',
												errorSummary : false
											}), summary],
									store : new Ext.data.JsonStore({
										url : __ctxPath + '/produce/listProjectAttendInfo.do',
										root : 'result',
										totalProperty : 'totalCounts',
										remoteSort : true,
										fields : ['id', 'name', 'duty', 'day1', 'day2', 'day3', 'day4', 'day5', 'day6', 'day7', 'day8', 'day9', 'day10',
												'day11', 'day12', 'day13', 'day14', 'day15', 'day16', 'day17', 'day18', 'day19', 'day20', 'day21', 'day22',
												'day23', 'day24', 'day25', 'day26', 'day27', 'day28', 'day29', 'day30', 'day31'],
										listeners : {
											'update' : function(value) {
												var r = this.getRange();
												var resultArray = [];
												var jsonArr = [];
												var sum = 0;
												var rlen = r.length;
												for (var i = 0; i < rlen; i++) {
													resultArray.push([]);
													if (r[i].fields) {
														var fields = r[i].fields;
														var flen = fields.length;
														var obj = {
															id : null,
															name : '',
															duty : '',
															day1 : 0,
															day2 : 0,
															day3 : 0,
															day4 : 0,
															day5 : 0,
															day6 : 0,
															day7 : 0,
															day8 : 0,
															day9 : 0,
															day10 : 0,
															day11 : 0,
															day12 : 0,
															day13 : 0,
															day14 : 0,
															day15 : 0,
															day16 : 0,
															day17 : 0,
															day18 : 0,
															day19 : 0,
															day20 : 0,
															day21 : 0,
															day22 : 0,
															day23 : 0,
															day24 : 0,
															day25 : 0,
															day26 : 0,
															day27 : 0,
															day28 : 0,
															day29 : 0,
															day30 : 0,
															day31 : 0
														}
														for (var z = 0; z < flen; z++) {
															var fieldValue = r[i].get(fields.keys[z]);
															if (Ext.isEmpty(fieldValue) || fieldValue == '请双击选择' || fieldValue == '请双击填写') {
																continue;
															}
															if (fields.keys[z] == 'id') {
																if (fieldValue != '') {
																	obj.id = fieldValue;
																}
															}else{
																eval("obj."+fields.keys[z]+" = fieldValue") ;
															}
														}
														jsonArr.push(obj);
													}
												}
												//删除空元素
												for (var i = 0; i < resultArray.length; i++) {
													if (resultArray[i].length == 0) {
														resultArray.splice(i, 1);
														i--;
													}
												}
												Ext.getCmp('projectAttend.dataList').setValue(Ext.util.JSON.encode(jsonArr));
												Ext.getCmp('projectAttend.resultGridData').setValue(resultArray);
											}
										}
									}),
									cm : new Ext.grid.ColumnModel({
										columns : [new Ext.grid.RowNumberer(), {
													dataIndex : 'id',
													width : 50,
													hidden : true
												}, {
													header : '姓名',
													dataIndex : 'name',
													editor : new Ext.grid.GridEditor(new Ext.form.TextField({
														allowBlank : false,
														id : 'flow_name',
														maxLength : 64,
														listeners : {
															focus : function(field) {
																if (field.getValue() == '请双击填写') {
																	field.setValue('');
																}
																if(Ext.getCmp('flow_duty').getValue()=='请双击填写'){
																	Ext.getCmp('flow_duty').setValue('');
																}
															}
														}
													})),
													summaryRenderer : function(v, params, data) {
														return '<font color="red">合计:</font>';
													}
												}, {
													header : '职务',
													dataIndex : 'duty',
													width : 120,
													editor : new Ext.grid.GridEditor(new Ext.form.TextField({
														id : 'flow_duty',
														allowBlank : false,
														maxLength : 64,
														listeners : {
															focus : function(field) {
																if (field.getValue() == '请双击填写') {
																	field.setValue('');
																}
																if(Ext.getCmp('flow_name').getValue()=='请双击填写'){
																	Ext.getCmp('flow_name').setValue('');
																}
															}
														}
													}))
												}, {
													header : '1',
													dataIndex : 'day1',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day1',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr,
														value:0
													})),
													renderer:thiz.renderStatus
												}, {
													header : '2',
													dataIndex : 'day2',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day2',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '3',
													dataIndex : 'day3',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day3',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '4',
													dataIndex : 'day4',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day4',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '5',
													dataIndex : 'day5',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day5',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '6',
													dataIndex : 'day6',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day6',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '7',
													dataIndex : 'day7',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day7',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '8',
													dataIndex : 'day8',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day8',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '9',
													dataIndex : 'day9',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day9',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '10',
													dataIndex : 'day10',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day10',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '11',
													dataIndex : 'day11',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day11',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '12',
													dataIndex : 'day12',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day12',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '13',
													dataIndex : 'day13',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day13',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '14',
													dataIndex : 'day14',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day14',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '15',
													dataIndex : 'day15',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day15',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '16',
													dataIndex : 'day16',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day16',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '17',
													dataIndex : 'day17',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day17',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '18',
													dataIndex : 'day18',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day18',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '19',
													dataIndex : 'day19',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day19',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '20',
													dataIndex : 'day20',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day20',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '21',
													dataIndex : 'day21',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day21',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '22',
													dataIndex : 'day22',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day22',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '23',
													dataIndex : 'day23',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day23',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '24',
													dataIndex : 'day24',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day24',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '25',
													dataIndex : 'day25',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day25',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '26',
													dataIndex : 'day26',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day26',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '27',
													dataIndex : 'day27',
													width : 70,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day27',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '28',
													dataIndex : 'day28',
													width : 70,
													hidden:new Date().getDaysInMonth()>=28?false:true,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day28',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '29',
													dataIndex : 'day29',
													width : 70,
													hidden:new Date().getDaysInMonth()>=29?false:true,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day29',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '30',
													dataIndex : 'day30',
													width : 70,
													hidden:new Date().getDaysInMonth()>=30?false:true,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day30',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}, {
													header : '31',
													dataIndex : 'day31',
													width : 70,
													hidden:new Date().getDaysInMonth()>=31?false:true,
													editor : new Ext.grid.GridEditor(new Ext.form.ComboBox({
														id : 'flow_day31',
														triggerAction : 'all',
														mode : 'local',
														store : statusArr
													})),
													renderer:thiz.renderStatus
												}],
										defaults : {
											sortable : false,
											menuDisabled : false,
											width : 100
										}
									}),
									tbar : new Ext.Toolbar({
										height : 30,
										items : [{
													iconCls : 'btn-add',
													text : '添加',
													xtype : 'button',
													handler : function() {
														var DataRecord = Ext.data.Record.create([{
																	name : 'id'
																}, {
																	name : 'name'
																}, {
																	name : 'duty'
																}, {
																	name : 'day1'
																}, {
																	name : 'day2'
																}, {
																	name : 'day3'
																}, {
																	name : 'day4'
																}, {
																	name : 'day5'
																}, {
																	name : 'day6'
																}, {
																	name : 'day7'
																}, {
																	name : 'day8'
																}, {
																	name : 'day9'
																}, {
																	name : 'day10'
																}, {
																	name : 'day11'
																}, {
																	name : 'day12'
																}, {
																	name : 'day13'
																}, {
																	name : 'day14'
																}, {
																	name : 'day15'
																}, {
																	name : 'day16'
																}, {
																	name : 'day17'
																}, {
																	name : 'day18'
																}, {
																	name : 'day19'
																}, {
																	name : 'day20'
																}, {
																	name : 'day21'
																}, {
																	name : 'day22'
																}, {
																	name : 'day23'
																}, {
																	name : 'day24'
																}, {
																	name : 'day25'
																}, {
																	name : 'day26'
																}, {
																	name : 'day27'
																}, {
																	name : 'day28'
																}, {
																	name : 'day29'
																}, {
																	name : 'day30'
																}, {
																	name : 'day31'
																}]);
														var store = Ext.getCmp('projectAttend.resultGridDataGrid').store;
														var grid = Ext.getCmp('projectAttend.resultGridDataGrid');

														var n1 = '请双击填写';

														var newRowData = {
															name : n1,
															duty : n1
														};
														var newRecord = new DataRecord(newRowData);
														store.add([newRecord]);

														grid.getSelectionModel().on('selectionchange', function(sm) {
															Ext.getCmp('projectAttend.removeBtn').setDisabled(sm.getCount() < 1);
														});
													}
												}, {
													iconCls : 'btn-del',
													text : '删除',
													id : 'projectAttend.removeBtn',
													xtype : 'button',
													handler : function() {
														var store = Ext.getCmp('projectAttend.resultGridDataGrid').store;
														var grid = Ext.getCmp('projectAttend.resultGridDataGrid');
														var s = grid.getSelectionModel().getSelections();
														for (var i = 0, r; r = s[i]; i++) {
															store.remove(r);
														}
														store.fireEvent("update");//更新事件
													}
												}]
									})
								}]
					}, {
						xtype : 'textfield',
						hidden : true,
						validator : function() {
							var resultGridData = Ext.getCmp('projectAttend.dataList').getValue();
							var errorLabel = Ext.getCmp("projectAttendErrorLabel");
							if (Ext.isEmpty(thizId)) {
								if (Ext.isEmpty(resultGridData)) {//为空显示提示
									errorLabel.setText('请填写考勤详情');
									return false;
								}
							}
							return true
						}
					}, {
						xtype : 'container',
						layout : 'column',
						height : 60,
						defaults : {
							xtype : 'label',
							style : {
								margin : '0px 0px 3px 0px'
							}
						},
						items : [{
									text : '备注：',
									width : 60
								}, {
									xtype : 'textarea',
									name : 'projectAttend.remark',
									id : 'remark',
									columnWidth : .98,
									height : 60
								}]
					}

			]
		});
		//加载表单对应的数据	
		this.initData();
		//初始化功能按钮
		this.buttons = [{
					text : '保存',
					iconCls : 'btn-save',
					handler : this.save.createCallback(this.formPanel, this)
				}, {
					text : '重置',
					iconCls : 'btn-reset',
					handler : this.reset.createCallback(this.formPanel)
				}, {
					text : '取消',
					iconCls : 'btn-cancel',
					handler : this.cancel.createCallback(this)
				}];
	},//end of the initcomponents
	/**
	 * 初始化数据
	 * 
	 * @param {}
	 *            formPanel
	 */
	initData : function() {
		if (Ext.isEmpty(this.id)) {
			var url = __ctxPath + '/produce/getByMonthProjectAttend.do';
			var data = ajaxSyncCall(url).data;
			if(!Ext.isEmpty(data)){
				this.id = data;
			}
		}
		//加载表单对应的数据	
		if (this.id != null && this.id != 'undefined') {
			Ext.getCmp('projectAttend.resultGridDataGrid').getStore().load({params:{
					'Q_projectAttend.id_L_EQ':this.id,
					start:0,
					limit:9999
			}});
			this.formPanel.getForm().load({
				deferredRender : false,
				url : __ctxPath + '/produce/getProjectAttend.do?id=' + this.id,
				waitMsg : '正在载入数据...',
				success : function(form, action) {
					var jsonData = Ext.util.JSON.decode(action.response.responseText);
					var res = jsonData.data;
					if (!Ext.isEmpty(res.project)) {
						Ext.getCmp("projectAttendProName").setValue(res.project.proName);
						Ext.getCmp("projectAttendProName").originalValue = res.project.proName;
						Ext.getCmp("projectAttend.project.id").setValue(res.project.id);
						Ext.getCmp("projectAttend.project.id").originalValue = res.project.id;
					}
					//Ext.getCmp("").originalValue=
				},
				failure : function(form, action) {
				}
			});
		}
	},
	/**
	 * 重置
	 * 
	 * @param {}
	 *            formPanel
	 */
	reset : function(formPanel) {
		formPanel.getForm().reset();
	},
	/**
	 * 取消
	 * 
	 * @param {}
	 *            window
	 */
	cancel : function(window) {
		window.close();
	},
	/**
	 * 保存记录
	 */
	save : function(formPanel, window) {
		if (formPanel.getForm().isValid()) {
			formPanel.getForm().submit({
				method : 'POST',
				waitMsg : '正在提交数据...',
				success : function(fp, action) {
					Ext.ux.Toast.msg('操作信息', '成功保存信息！');
					var gridPanel = Ext.getCmp('ProjectAttendGrid');
					if (gridPanel != null) {
						gridPanel.getStore().reload();
					}
					window.close();
				},
				failure : function(fp, action) {
					Ext.MessageBox.show({
						title : '操作信息',
						msg : '信息保存出错，请联系管理员！',
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
					//window.close();
				}
			});
		}
	}//end of save
	,renderStatus:function(value,meta,record){
			var statusArr = [[0, '未填写'], [1, '到勤'], [2, '事假'], [3, '旷工'], [4, '迟到'], [5, '休息']];
			var str = "";
			Ext.each(statusArr,function(item,i){
				if(item[0]==value){
					str = item[1];
					return;
				}
			});
			return str;
		}
});